package day11;

import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author oldliu
 * @since 1.0
 */
public class XMLModify {
    public static void main(String[] args) {
        SAXReader reader = new SAXReader();
        Document document = null;
        try {
            document = reader.read(new File("课工场76.xml"));
        } catch (DocumentException e) {
            e.printStackTrace();

        }
        //根据学号，改手机号和姓名
        int no = 1002;
        Element e = (Element) document.selectSingleNode("/javaclass/student[@no='" + no + "']");
        if (e == null) {
            System.out.println("没有");
            return;
        }
        e.selectSingleNode("name").setText("猪悟能");
        e.selectSingleNode("phone").setText("13865771023");
        //写回xml
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("utf-8");
        try {
            XMLWriter xm = new XMLWriter(new FileWriter("课工场76.xml"), format);
            xm.write(document);
            xm.close();
            System.out.println("xie ru chenggong");
        } catch (IOException ee) {
            ee.printStackTrace();
        }
    }
}

